var params = {};
var current = {};
var defaultPageSize = 10;
var currentPage = 1;
var pageSize = defaultPageSize;
var option = {};



//物料类别需要用到的变量
var wl=0;
var matParams = {};
var matCurrent = {};
var matDefaultPageSize = 10;
var matCurrentPage = 1;
var matPageSize = matDefaultPageSize;
var matOption = {};
function finishedGoods(obj){
	wl=obj;
	matOption.url = webUrl+'chwMateriel/findchwMateriel.do';
	matOption.type="wuliao";				//div名称前缀user_dialogbox
	matOption.table="wuliaoGrid";	//div中table表名
	matOption.obj="wuliao";				//回调方法中传值做标识
	//matParams.typeCode="MaterielCode";
	var materielName=$("#materielName").val();
	//var materielTypeName=$("#materielTypeName").val();
	if(materielName!=""){
		matParams.materielName=materielName;
	}
	/*if(materielTypeName!=""){
		matParams.materielTypeName=materielTypeName;
	}*/
	matOption.columns = [
	                  {field:'materielId',checkbox:true},
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'materielName',title:'物料名称',align:'center',width:150,sortable:true},
			          {field:'segment1',title:'物料编码',align:'center',width:150,sortable:true},
			          {field:'materielVersionNum',title:'版本编号',align:'center',width:80,sortable:true},
			          {field:'materielVersionName',title:'版本名称',align:'center',width:150,sortable:true},
			          {field:'materielVersionModel',title:'版本型号',align:'center',width:150,sortable:true},
			      ];
	matOption.onSelect=function(rowIndex, rowData){
		$("#finishedGoodsName_"+wl).val(rowData.materielName);
		$("#finishedGoodsId_"+wl).val(rowData.materielId);
		$("#segment1_"+wl).val(rowData.segment1);
		$("#materielVersionNum_"+wl).val(rowData.materielVersionNum);
		$("#materielVersionName_"+wl).val(rowData.materielVersionName);
		$("#materielVersionModel_"+wl).val(rowData.materielVersionModel);
		
	};
	matLov(matOption);
}


$("ul.tabtit").on("click",function(event){
	$('.main_panel').load(webUrl+"public/page/contract/contractQuery.html");
});

function emptyContract(){
	$('.main_panel').load(webUrl+"public/page/contract/contractQuery.html");
}

$(function(){
	$('.add').on('click',saveContractHeader);
	$('.empty').on('click',emptyContract);
	$('.check').on('click',queryParam);
	
	$("#salesmanName").bind('click',function(){employee()});
});



function queryParam(){
	finishedGoods(wl);
}

function queryCustomer(){
	option.url = webUrl+'customer/findChwCustomer.do';
	option.type="user";				//div名称前缀user_dialogbox
	option.table="customerGrid";	//div中table表名
	option.obj="customer";				//回调方法中传值做标识
	option.columns = [
	                  {field:'customerId',checkbox:true},
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'customerName',title:'客户名称',align:'center',width:150,sortable:true},
			          {field:'customerPhone',title:'联系方式',align:'center',width:150,sortable:true},
			          {field:'remark',title:'备注',align:'center',width:150,sortable:true},
			      ];
	option.onSelect=function(rowIndex, rowData){ $("#customerName").val(rowData.customerName); $("#customerId").val(rowData.customerId);};
	Lov(option);
}


/*function querySalesman(){
	$("#salesmanName").bind('click',function(){lovEmployee('buyer')});
}*/


function setPager(pager,total){
	pager.pagination({
		total:total,
		pageNumber:currentPage,
	    pageSize:defaultPageSize,
	    layout:['first','prev','links','next','last'],
	    onSelectPage:function(cpage, psize){
	    	currentPage=cpage;
	    	pageSize=psize;
	    	queryCustomer();
		}
	});
}

function goback(obj){
	if(obj=="customer"){
		var customerId= $('input[name="customerId"]:checked').val();
		$("#customerId").val(customerId);
	}
}

function saveContractHeader(){
	var json = {};
	json.chwContractHeader = {};
	json.chwContractHeader.contractNum=$("#contractNum").val();
	if($("#signingDate").datebox('getValue')!=""){
		json.chwContractHeader.signingDate=$("#signingDate").datebox('getValue');
	}
	if($("#implementDate").datebox('getValue')!=""){
		json.chwContractHeader.implementDate=$("#implementDate").datebox('getValue');
	}
	if($("#paramValue").datebox('getValue')!=""){
		json.chwContractHeader.completionDate=$("#paramValue").datebox('getValue');
	}
	json.chwContractHeader.status=$("#status").combobox("getValue");
	json.chwContractHeader.remark=$("#remark").val();
	json.chwContractHeader.customerId=$("#customerId").val();
	json.chwContractHeader.paymentTermId=$("#paymentTermId").val();
	json.chwContractHeader.customerName=$("#customerName").val();
	json.chwContractHeader.salesmanId=$("#salesmanId").val();
	json.chwContractHeader.salesmanName=$("#salesmanName").val();
	json.chwContractLine = [];
	var valueTable = document.getElementById("fixedThead");
	var table_tr = valueTable.getElementsByTagName('tr');
	for ( var i = 1; i < table_tr.length; i++) {
		var inputParam = table_tr[i].getElementsByTagName('input');
		var selectParam = table_tr[i].getElementsByTagName('select');
		var Values = {};
		values={
				"contractLineNum":inputParam[1].value,
				"finishedGoodsId":inputParam[3].value,
				"quantity":inputParam[8].value,
				"salesUnitPrice":inputParam[9].value,
				"constructionCost1":inputParam[10].value,
				/*"constructionCost2":inputParam[6].value,*/
				"constructionCost3":inputParam[11].value,
				"totalAmount":inputParam[12].value,
				/*"taxRate":selectParam[0].value,
				"taxmoneyNumber":inputParam[9].value,
				"intaxmoneyNumber":inputParam[10].value,*/
				"remark":inputParam[13].value
		};
		json.chwContractLine.push(values);
	}
	json=JSON.stringify(json);
	if( $("#signingDate").datebox('getValue')=="" || $("#status").combobox("getValue")==""){
		alert("*号为必填项！");
	}else if(checknull("contractNum") && checknull("customerId") && checknull("paymentTermId")
			){
		$.ajax({
			url: webUrl+'contractHeader/saveChwHeader.do',
			type:'post',
			data:{param:json}
		}).done(function(data){
			var result = data.bo;
			if(result==0){
				$.messager.confirm('提示 ', '保存成功！', function(r){
				    $('.main_panel').load(webUrl+"public/page/contract/contractQuery.html");
				});
			}else{
				$.messager.alert('提示 ', '保存失败！', 'info');
			}
		});
	}
}



//付款条款需要用到的变量
var paymentParams = {};
var paymentCurrent = {};
var paymentDefaultPageSize = 10;
var paymentCurrentPage = 1;
var paymentPageSize = paymentDefaultPageSize;
var paymentOption = {};
function payment(){
	paymentOption.url = webUrl+'sysParams/getSysParamsList.do';
	paymentOption.type="fk";				//div名称前缀user_dialogbox
	paymentOption.table="fkGrid";	//div中table表名
	paymentOption.obj="fk";				//回调方法中传值做标识
	paymentParams.typeCode="FK";
	var paramValue=$("#paymentTermName").val();
	if(paramValue!=""){
		paymentParams.paramValue=paramValue;
	}
	paymentOption.columns = [
	                  {field:'id',checkbox:true},
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'param_value',title:'付款名称',align:'center',width:150,sortable:true},
			      ];
	paymentOption.onSelect=function(rowIndex, rowData){ $("#paymentTermId").val(rowData.param_value); };
	paymentLov(paymentOption);
}

//人员LOV用到的变量 (采购员)
var employeeParams = {};
var employeeCurrent = {};
var employeeDefaultPageSize = 10;
var employeeCurrentPage = 1;
var employeePageSize = employeeDefaultPageSize;
var employeeOption = {};
function employee(){
	employeeOption.url = webUrl+'chwEmployee/findEntity.do';
	employeeOption.type="buyer";				//div名称前缀user_dialogbox
	employeeOption.table="buyerGrid";	//div中table表名
	employeeParams.typeCode="FK";
	var paramValue=$("#employeeNameQuery").val();
	if(paramValue!=""){
		employeeParams.employeeName=paramValue;
	}
	employeeOption.columns = [ {field : 'employeeId',checkbox : true	}, 
	{field : '_itemNo',	title : '序号',	align : 'center',	width : 50,	formatter : formatId}, 
	{field : 'employeeName',title : '姓名',	align : 'center',	width : 150,sortable : true	}, 
	{field : 'employeeSex',	title : '性别',	align : 'center',	width : 150,sortable : true,}, ];
	employeeOption.onSelect=function(rowIndex, rowData){ $("#salesmanName").val(rowData.employeeName);$("#salesmanId").val(rowData.employeeId); };
	employeeLov(employeeOption);
}